package com.bawei.rk_01

import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.rk_01.Login.LoginViewModel
import com.bawei.rk_01.Room.AppDBUtils
import com.bawei.rk_01.Room.MoneyEntity
import com.bawei.rk_01.databinding.ActivityLoginBinding
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils

@Route(path = "/app/LoginActivity")

class LoginActivity : BaseAcitivity<ActivityLoginBinding, LoginViewModel>() {
    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun initView() {

        //获取富文本
        val s = binding.cb1.text.toString()
        val spannableStringBuilder = SpannableStringBuilder(s)
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebActivity").withString("url","https://www.baidu.com").navigation()
            }
        }
        val clickableSpan2 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebActivity").withString("url","https://stzb.163.com/bingpc").navigation()
            }
        }
        spannableStringBuilder.setSpan(clickableSpan,3,9,SpannableStringBuilder.SPAN_INTERMEDIATE)
        spannableStringBuilder.setSpan(clickableSpan2,10,16,SpannableStringBuilder.SPAN_INTERMEDIATE)
        binding.cb1.movementMethod = LinkMovementMethod.getInstance()
        binding.cb1.text = spannableStringBuilder

        binding.bt1.setOnClickListener {
            //账号密码不为空
            if (binding.et1.text.toString().isEmpty() && binding.et2.text.toString().isEmpty()){
                ToastUtils.showLong("账号密码不能为空")
                return@setOnClickListener
            }
            //判断手机号是否正确
            if (!RegexUtils.isMobileExact(binding.et1.text.toString())){
                ToastUtils.showLong("手机号码格式不正确")
                return@setOnClickListener
            }
            //判断是否勾选同意天空
            if (!binding.cb1.isChecked){
                ToastUtils.showLong("请同意用户协议")
                return@setOnClickListener
            }
            viewModel.login(binding.et1.text.toString(),binding.et2.text.toString())
        }

        //获取验证码
        binding.bt2.setOnClickListener {
            viewModel.getCode(binding.et1.text.toString())
        }

        //验证码登录
        binding.bt3.setOnClickListener {
            viewModel.codelogin(binding.et3.text.toString(),binding.et1.text.toString())
        }

    }

    override fun initData() {

        viewModel.loginsuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("何易谦，别流口水了")
                SPUtils.getInstance().put("token",it.data.token)
            }
        }

        viewModel.loginfail.observe(this){
            ToastUtils.showLong(it)
        }

        viewModel.codesuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("获取验证码成功")
                val substring = it.data.content.substring(8, 14)
                binding.et3.setText(substring)
            }
        }

        viewModel.codefail.observe(this){
            ToastUtils.showLong(it)
        }

        viewModel.codeligonsuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("验证码登录成功")
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                SPUtils.getInstance().put("userid",it.data.uid)
                ARouter.getInstance().build("/app/MainActivity").navigation()

                //判断用户是否存在
                val query = AppDBUtils.appDataBase.moneyDao().query(it.data.uid)
                if (query == null){
                    AppDBUtils.appDataBase.moneyDao().insert(MoneyEntity(it.data.uid,2000f))
                }

            }
        }

        viewModel.codeloginfail.observe(this){
            ToastUtils.showLong(it)
        }

    }

}